<?php

declare(strict_types=1);

namespace App\Job;

use App\Constants\MissionType;
use App\Services\Mission\MissionQueueService;
use App\Utils\LoggerUtil;
use Hyperf\AsyncQueue\Job;
use Throwable;

class MissionJob extends Job
{
    private mixed $params;

    public function __construct($params)
    {
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象
        $this->params = $params;
    }

    public function handle()
    {
        $this->job($this->params);
    }

    private function job(mixed $origin)
    {
        $mission = new MissionQueueService();
        $missionProcessor = [
            MissionType::MALE_CALL => 'archiveCallMission',
            MissionType::MALE_COMMENT => 'archiveCommentMission',
            MissionType::MALE_HEAD_IMAGE => 'archiveHeadImageMission',
            MissionType::FEMALE_HEAD_IMAGE => 'archiveHeadImageMission',
            MissionType::MALE_LIKE => 'archiveLikeMission',
            MissionType::MALE_PROFILE => 'archiveProfileMission',
            MissionType::MALE_REPLY => 'archiveReplyMission',
            MissionType::MALE_AUDIO => 'archiveAudioMission',
            MissionType::MALE_TEASE => 'archiveTeaseMission',
            MissionType::MALE_TOPIC => 'archiveTopicMission',
            MissionType::FEMALE_UPLOAD_ALBUM => 'archiveUploadAlbumMission',
            MissionType::FEMALE_UPLOAD_VIDEO => 'archiveUploadVideoMission',
        ];

        try {
            $data = json_decode($origin, true);
            extract($data);

            $func = $missionProcessor[$type];
            $mission->$func($uid, $sex);
        } catch (Throwable $e) {
            LoggerUtil::write('mission', [
                $e->getFile(), $e->getLine(), $e->getMessage()
            ], 'MissionJob');
        }
    }
}
